const { MongoClient } = require("mongodb");
const mongoose = require('mongoose');
const express = require('express');
const path = require('path');
const fs = require('fs-extra');
const config = require('./config/config');
const authRoutes = require('./routes/authRoutes');
const modelRoutes = require('./routes/modelRoutes');
const publicModelRoutes = require('./routes/publicModelRoutes');

// 确保存储目录存在
fs.ensureDirSync(config.storage.basePath);

// 数据库连接字符串（根据实际情况修改）
const uri = "mongodb://localhost:27017"; // 本地 MongoDB 服务器

// 创建客户端实例
const client = new MongoClient(uri, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

// 连接MongoDB
mongoose.connect(config.mongoURI)
    .then(() => console.log('MongoDB连接成功'))
    .catch(err => console.error('MongoDB连接失败:', err));

// 解析JSON请求体
const app = express();
app.use(express.json());
const cors = require('cors');
app.use(cors());
// 注册认证路由
app.use('/api/auth', authRoutes);
// 注册模型路由
app.use('/api/models', modelRoutes);
// 注册公共模型路由
app.use('/api/public-models', publicModelRoutes);

// ... 现有代码 ...

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`服务器运行在端口 ${PORT}`);
});